<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class edit_session_dat  {

	var $title = FALSE;
	var $name = 'edit_session_dat';
	var $entry = FALSE;

	var $dat_id = FALSE;

	var $_type = FALSE;
	var $_unit = FALSE;
	var $_name = FALSE;
	var $_description = FALSE;
	var $_value = FALSE;
	var $_comment = FALSE;

	var $combo2type_id = array();
	var $data_types = array();
	
	function __construct($title="Edit [session] Data",$name=FALSE) {
		$this->title = settings::xlate($title);	
		if ( $name ) $this->name = $name;
	}


	function edit($entry_id,$dat_id=FALSE) {

		$this->dat_id = $dat_id;

		$cnf = new conf_timesheet(INIFILE);

		$glade = new GladeXML(PATH_RES.'edit_session_dat.glade','edit_session_dat');
		$glade->signal_autoconnect_instance($this);
		
		$dialog = $glade->get_widget('edit_session_dat');
		$dialog->set_title($this->title);


		$this->_type = $glade->get_widget('dat_type');
		$this->_name = $glade->get_widget('name');
		$this->_description = $glade->get_widget('description');
		$this->_value = $glade->get_widget('value');
		$this->_unit = $glade->get_widget('unit');
		$this->_comment = $glade->get_widget('comment');


		// Defaults
		$type = '';
		$description = '';
		$value = '';
		$comment = '';

		$c = new settings(platform::app_data('Tazu-Timesheet').'config.db');

		// Edit an existing record

		if ( $this->dat_id ) {

			$ts = new Timesheet($cnf->ini['TIMESHEET']['current']);


			$type = $ts->getDatType($dat_id);

			$name = $c->getDatName($type);
			$description = $c->getDatDescription($type);
			$unit = $c->getDatUnit($type);

			$value = $ts->getDatValue($dat_id);
			$comment = $ts->getDatComment($dat_id);

			$ts->close();
			unset($ts);
			
		}
			
		$this->_description->set_label($description);
		$this->_value->set_text($value);
		$this->_unit->set_label($unit);
		$this->_comment->set_text($comment);

		// Combo
		// Create a model
		if (defined("GObject::TYPE_STRING")) {
		    $model = new GtkListStore(GObject::TYPE_STRING);
		} else {
		    $model = new GtkListStore(GObject::TYPE_STRING);
		}
		$this->_type->set_model($model);
		$cellRenderer = new GtkCellRendererText(); // note 2
		$this->_type->pack_start($cellRenderer);
		$this->_type->set_attributes($cellRenderer, 'text', 0); // note 3 


		$this->data_types = $c->getDataTypes();

		$this->combo2type_id = array();
		$i = 0;

		$selected = 0;
		foreach ( $this->data_types as $idx => $dat) {
			$model->append(array($dat['name']));
			$this->combo2type_id[$i] = $dat['id'];
			if ( $dat['id'] == $type ) {
				$selected = $i;
			}
			$i++;
		}
		
		if ( $selected >= 0 ) {

			$this->_type->set_active($selected);
		}
		unset($c);


//		$this->entry = $glade->get_widget('entry');
//		$this->entry->set_width_chars($width_chars);


        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/

        $dialog->show_all();
        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {


			$type = $this->combo2type_id[$this->_type->get_active()];
			$value = $this->_value->get_text();
			$comment = $this->_comment->get_text();

			$ok = ( $type <> '' && $value <> '' );		// validation

			if ( $ok ) {
					
				$ts = new Timesheet($cnf->ini['TIMESHEET']['current']);

				if ( $this->dat_id ) {
					// Existing record


					$ts->updateDat($dat_id,'type',$type);							
					$ts->updateDat($dat_id,'value',$value);							
					$ts->updateDat($dat_id,'comment',$comment);							

				}
				else {
					// New record
					$dat_id = $ts->addDat($entry_id,$type,$value,$comment);
				}
						
				$ts->close();
				unset( $ts );

			}


			$result = $dat_id;

		}
		else {

			$result = FALSE;		// cancel
		}

        $dialog->destroy();

		return($result);

    }


	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }
    
	/**
	*	Type combo clicked
	*	
	*	@param	
	*	@return	
	*	
	*/
	function onTypeChanged() {

		$sel = $this->combo2type_id[$this->_type->get_active()];

		$name = $this->data_types[$sel]['name'];
		$description = $this->data_types[$sel]['description'];
		$unit = $this->data_types[$sel]['unit'];

		$this->_name->set_label("<b>$name</b>");
		$this->_description->set_label($description);
		$this->_unit->set_label($unit);
	
	}

}

?>